package com.sff.site.utils {
	
	import com.sff.core.sff_internal;
	import com.sff.site.tree.SiteTreePath;
	import com.sff.site.tree.TreesProviderClass;
	
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.net.navigateToURL;

	public class Link {
		
		public static const INT : String = "int";
		public static const EXT : String = "ext";
		
		public var target : String = "_blank";

		private var _type : String;
		private var _src : String;
		private var _params : URLVariables;
		private var _method : String = URLRequestMethod.GET;
		
		public function get type() : String {
			return _type;
		}

		public function get src() : String {
			return _src;
		}

		/**
		 * 		<link type="int|ext" target="_blank|_self|_parent|..."</link>;
		 * 		<link type="int">
		 *			<src><![CDATA[]]></src>
		 *			<text><![CDATA[]]></text>
		 *			<params method="POST">
		 *				<p name="p1">param1</p>
		 *			</params>
		 *		</link>
		 */
		public function Link( desc : XML ) {
			_parse( desc );
		}
		
		public function get params() : URLVariables {
			return _params;
		}
		
		public function set params(params : URLVariables) : void {
			_params = params;
		}
		
		public function set method(value : String) : void {
			_method =  value;	
		}
		

		public function get method( ) : String {
			return _method;
		}

		
		private function _parse( desc : XML ) : void {
			_type = desc.@type.toString();
			target = desc.@target.toString();
			_src = desc.src.text( );
			
			if( desc.params.length() > 0 ) {
				_method = desc.params.@method.toString();
				_params = new URLVariables();
				desc.params.p.( _params[ @name ] == text() );
			}
		}

		public function navigate() : void {			
			if( _type == EXT ) {
				var req : URLRequest = new URLRequest( src );
				req.data = _params;
				req.method = _method;
				navigateToURL( req, target );
			
			} else if( _type == INT ) {
				var path : SiteTreePath = new SiteTreePath( src );
				if(params) {
					path.setParams( params );
				}
				path.activate();			
			}			
		}				
	}
}
